Micron Document
🎖️GitЯра🎖️

Node / meshtastic / Meshtastic-Android / files / core / service / src / commonMain / kotlin / org / meshtastic / core / service / MeshServiceOrchestrator.kt

Displaying Raw • Download

core/service/src/commonMain/kotlin/org/meshtastic/core/service/MeshServiceOrchestrator.kt cef12c39ddda652eec872a2ccffa52a2665bd2ec (cef12c39) Text, 7.87 KB

T8b949e/*
* Copyright (c) 2026 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
Tff7b72package T7ee787org.meshtastic.core.service

Tff7b72import T7ee787co.touchlab.kermit.Logger
Tff7b72import T7ee787co.touchlab.kermit.Severity
Tff7b72import T7ee787kotlinx.atomicfu.atomic
Tff7b72import T7ee787kotlinx.coroutines.CoroutineScope
Tff7b72import T7ee787kotlinx.coroutines.SupervisorJob
Tff7b72import T7ee787kotlinx.coroutines.cancel
Tff7b72import T7ee787kotlinx.coroutines.flow.launchIn
Tff7b72import T7ee787kotlinx.coroutines.flow.onEach
Tff7b72import T7ee787kotlinx.coroutines.isActive
Tff7b72import T7ee787kotlinx.coroutines.launch
Tff7b72import T7ee787org.koin.core.annotation.Single
Tff7b72import T7ee787org.meshtastic.core.common.database.DatabaseManager
Tff7b72import T7ee787org.meshtastic.core.common.util.handledLaunch
Tff7b72import T7ee787org.meshtastic.core.di.CoroutineDispatchers
Tff7b72import T7ee787org.meshtastic.core.repository.MeshConnectionManager
Tff7b72import T7ee787org.meshtastic.core.repository.MeshMessageProcessor
Tff7b72import T7ee787org.meshtastic.core.repository.MeshNotificationManager
Tff7b72import T7ee787org.meshtastic.core.repository.NodeManager
Tff7b72import T7ee787org.meshtastic.core.repository.RadioInterfaceService
Tff7b72import T7ee787org.meshtastic.core.repository.ServiceStateWriter
Tff7b72import T7ee787org.meshtastic.core.repository.TakPrefs
Tff7b72import T7ee787org.meshtastic.core.takserver.TAKMeshIntegration
Tff7b72import T7ee787org.meshtastic.core.takserver.TAKServerManager

T8b949e/**
* Platform-agnostic orchestrator for the mesh service lifecycle.
*
* Extracts the startup wiring previously embedded in Android's `MeshService.onCreate()` into a reusable component. Both
* Android's foreground `Service` and the Desktop `main()` function can use this to start/stop the mesh service graph.
*
* All injected dependencies are `commonMain` interfaces with real implementations in `core:data`.
*/
Tf0883e@SuppressTb4b4b4(Ta5d6ff"Ta5d6ffLongParameterListTa5d6ff"Tb4b4b4)
Tf0883e@Single
Tff7b72class T56d364MeshServiceOrchestratorTb4b4b4(
Tff7b72private Tff7b72val Te6edf3radioInterfaceServiceTb4b4b4: Te6edf3RadioInterfaceServiceTb4b4b4,
Tff7b72private Tff7b72val Te6edf3serviceStateWriterTb4b4b4: Te6edf3ServiceStateWriterTb4b4b4,
Tff7b72private Tff7b72val Te6edf3nodeManagerTb4b4b4: Te6edf3NodeManagerTb4b4b4,
Tff7b72private Tff7b72val Te6edf3messageProcessorTb4b4b4: Te6edf3MeshMessageProcessorTb4b4b4,
Tff7b72private Tff7b72val Te6edf3serviceNotificationsTb4b4b4: Te6edf3MeshNotificationManagerTb4b4b4,
Tff7b72private Tff7b72val Te6edf3takServerManagerTb4b4b4: Te6edf3TAKServerManagerTb4b4b4,
Tff7b72private Tff7b72val Te6edf3takMeshIntegrationTb4b4b4: Te6edf3TAKMeshIntegrationTb4b4b4,
Tff7b72private Tff7b72val Te6edf3takPrefsTb4b4b4: Te6edf3TakPrefsTb4b4b4,
Tff7b72private Tff7b72val Te6edf3databaseManagerTb4b4b4: Te6edf3DatabaseManagerTb4b4b4,
Tff7b72private Tff7b72val Te6edf3connectionManagerTb4b4b4: Te6edf3MeshConnectionManagerTb4b4b4,
Tff7b72private Tff7b72val Te6edf3dispatchersTb4b4b4: Te6edf3CoroutineDispatchersTb4b4b4,
Tb4b4b4) Tb4b4b4{
T8b949e// Per-start coroutine scope. A fresh scope is created on each start() and cancelled on stop(), so all collectors
T8b949e// launched from start() are torn down cleanly and do not accumulate across start/stop/start cycles.
T8b949e// Held in an atomic ref so concurrent start()/stop() callers serialize on compareAndSet rather than racing through
T8b949e// a check-then-set on a plain var.
Tff7b72private Tff7b72val Te6edf3scopeRef Tff7b72= Te6edf3atomicTff7b72<Te6edf3CoroutineScope?Tff7b72>Tb4b4b4(Tff7b72nullTb4b4b4)

T8b949e/** Whether the orchestrator is currently running. */
Tff7b72val Te6edf3isRunningTb4b4b4: Tffa657Boolean
Tff7b72getTb4b4b4(Tb4b4b4) Tff7b72= Te6edf3scopeRefTb4b4b4.Te6edf3valueTff7b72?.Te6edf3isActive Tff7b72=Tff7b72= Tff7b72true

T8b949e/**
* Starts the mesh service components and wires up data flows.
*
* This is the KMP equivalent of `MeshService.onCreate()`. It connects to the radio and wires incoming radio data to
* the message processor.
*/
Tff7b72fun Td2a8ffstartTb4b4b4(Tb4b4b4) Tb4b4b4{
Tff7b72val Te6edf3newScope Tff7b72= Te6edf3CoroutineScopeTb4b4b4(Te6edf3SupervisorJobTb4b4b4(Tb4b4b4) Tff7b72+ Te6edf3dispatchersTb4b4b4.Te6edf3defaultTb4b4b4)
T8b949e// Atomic claim — if another thread already installed a live scope, abandon ours.
Tff7b72if Tb4b4b4(Tff7b72!Te6edf3scopeRefTb4b4b4.Te6edf3compareAndSetTb4b4b4(Te6edf3expect Tff7b72= Tff7b72nullTb4b4b4, Te6edf3update Tff7b72= Te6edf3newScopeTb4b4b4)Tb4b4b4) Tb4b4b4{
Tff7b72val Te6edf3existing Tff7b72= Te6edf3scopeRefTb4b4b4.Te6edf3value
Tff7b72if Tb4b4b4(Te6edf3existingTff7b72?.Te6edf3isActive Tff7b72=Tff7b72= Tff7b72trueTb4b4b4) Tb4b4b4{
Te6edf3LoggerTb4b4b4.Te6edf3d Tb4b4b4{ Ta5d6ff"Ta5d6ffstart() called while already running, ignoringTa5d6ff" Tb4b4b4}
Tff7b72return
Tb4b4b4}
T8b949e// The slot held a dead scope (post-stop). CAS-replace it to avoid racing with another caller.
Tff7b72if Tb4b4b4(Tff7b72!Te6edf3scopeRefTb4b4b4.Te6edf3compareAndSetTb4b4b4(Te6edf3expect Tff7b72= Te6edf3existingTb4b4b4, Te6edf3update Tff7b72= Te6edf3newScopeTb4b4b4)Tb4b4b4) Tb4b4b4{
Te6edf3LoggerTb4b4b4.Te6edf3d Tb4b4b4{ Ta5d6ff"Ta5d6ffstart() lost race replacing dead scope, ignoringTa5d6ff" Tb4b4b4}
Te6edf3newScopeTb4b4b4.Te6edf3cancelTb4b4b4(Tb4b4b4)
Tff7b72return
Tb4b4b4}
Tb4b4b4}

Te6edf3LoggerTb4b4b4.Te6edf3i Tb4b4b4{ Ta5d6ff"Ta5d6ffStarting mesh service orchestratorTa5d6ff" Tb4b4b4}

T8b949e// Drop any bytes that piled up in the service's receivedData channel since the last stop(). The channel
T8b949e// outlives the orchestrator's per-start scope, so without this drain a stop/start cycle would replay stale
T8b949e// packets ahead of the fresh session's firmware handshake.
Te6edf3radioInterfaceServiceTb4b4b4.Te6edf3resetReceivedBufferTb4b4b4(Tb4b4b4)

Te6edf3serviceNotificationsTb4b4b4.Te6edf3initChannelsTb4b4b4(Tb4b4b4)
Te6edf3connectionManagerTb4b4b4.Te6edf3updateStatusNotificationTb4b4b4(Tb4b4b4)

T8b949e// Observe TAK server pref to start/stop
Te6edf3takPrefsTb4b4b4.Te6edf3isTakServerEnabled
Tb4b4b4.Te6edf3onEach Tb4b4b4{ Te6edf3isEnabled Tff7b72-Tff7b72>
Tff7b72if Tb4b4b4(Te6edf3isEnabled Tff7b72&Tff7b72& Tff7b72!Te6edf3takServerManagerTb4b4b4.Te6edf3isRunningTb4b4b4.Te6edf3valueTb4b4b4) Tb4b4b4{
Te6edf3LoggerTb4b4b4.Te6edf3i Tb4b4b4{ Ta5d6ff"Ta5d6ffTAK Server enabled by preference, starting integrationTa5d6ff" Tb4b4b4}
Te6edf3takMeshIntegrationTb4b4b4.Te6edf3startTb4b4b4(Te6edf3newScopeTb4b4b4)
Tb4b4b4} Tff7b72else Tff7b72if Tb4b4b4(Tff7b72!Te6edf3isEnabled Tff7b72&Tff7b72& Te6edf3takServerManagerTb4b4b4.Te6edf3isRunningTb4b4b4.Te6edf3valueTb4b4b4) Tb4b4b4{
Te6edf3LoggerTb4b4b4.Te6edf3i Tb4b4b4{ Ta5d6ff"Ta5d6ffTAK Server disabled by preference, stopping integrationTa5d6ff" Tb4b4b4}
Te6edf3takMeshIntegrationTb4b4b4.Te6edf3stopTb4b4b4(Tb4b4b4)
Tb4b4b4}
Tb4b4b4}
Tb4b4b4.Te6edf3launchInTb4b4b4(Te6edf3newScopeTb4b4b4)

Te6edf3newScopeTb4b4b4.Te6edf3handledLaunch Tb4b4b4{
T8b949e// Ensure the per-device database is active before the radio connects.
T8b949e// On Android this is handled by MeshUtilApplication.init(); on Desktop (and any
T8b949e// future KMP host) the orchestrator is the first entry point, so it must initialize
T8b949e// the database here. Without this, DatabaseManager._currentDb stays null and all
T8b949e// Room writes via withDb() are silently dropped — causing ourNodeInfo to remain null
T8b949e// after the handshake completes.
Te6edf3databaseManagerTb4b4b4.Te6edf3switchActiveDatabaseTb4b4b4(Te6edf3radioInterfaceServiceTb4b4b4.Te6edf3getDeviceAddressTb4b4b4(Tb4b4b4)Tb4b4b4)
Te6edf3LoggerTb4b4b4.Te6edf3i Tb4b4b4{ Ta5d6ff"Ta5d6ffPer-device database initialized, connecting radioTa5d6ff" Tb4b4b4}
Te6edf3radioInterfaceServiceTb4b4b4.Te6edf3connectTb4b4b4(Tb4b4b4)
Tb4b4b4}

Te6edf3radioInterfaceServiceTb4b4b4.Te6edf3receivedData
Tb4b4b4.Te6edf3onEach Tb4b4b4{ Te6edf3bytes Tff7b72-Tff7b72> Te6edf3messageProcessorTb4b4b4.Te6edf3handleFromRadioTb4b4b4(Te6edf3bytesTb4b4b4, Te6edf3nodeManagerTb4b4b4.Te6edf3myNodeNumTb4b4b4.Te6edf3valueTb4b4b4) Tb4b4b4}
Tb4b4b4.Te6edf3launchInTb4b4b4(Te6edf3newScopeTb4b4b4)

Te6edf3radioInterfaceServiceTb4b4b4.Te6edf3connectionError
Tb4b4b4.Te6edf3onEach Tb4b4b4{ Te6edf3errorMessage Tff7b72-Tff7b72> Te6edf3serviceStateWriterTb4b4b4.Te6edf3setErrorMessageTb4b4b4(Te6edf3errorMessageTb4b4b4, Te6edf3SeverityTb4b4b4.Te6edf3WarnTb4b4b4) Tb4b4b4}
Tb4b4b4.Te6edf3launchInTb4b4b4(Te6edf3newScopeTb4b4b4)

Te6edf3nodeManagerTb4b4b4.Te6edf3loadCachedNodeDBTb4b4b4(Tb4b4b4)
Tb4b4b4}

T8b949e/**
* Stops the mesh service components and cancels the coroutine scope.
*
* This is the KMP equivalent of `MeshService.onDestroy()`.
*/
Tff7b72fun Td2a8ffstopTb4b4b4(Tb4b4b4) Tb4b4b4{
Te6edf3LoggerTb4b4b4.Te6edf3i Tb4b4b4{ Ta5d6ff"Ta5d6ffStopping mesh service orchestratorTa5d6ff" Tb4b4b4}
T8b949e// Guard stop() so we don't emit a spurious "stopped" log when TAK was never started
Tff7b72if Tb4b4b4(Te6edf3takServerManagerTb4b4b4.Te6edf3isRunningTb4b4b4.Te6edf3valueTb4b4b4) Tb4b4b4{
Te6edf3takMeshIntegrationTb4b4b4.Te6edf3stopTb4b4b4(Tb4b4b4)
Tb4b4b4}
T8b949e// Best-effort polite goodbye on service teardown (onDestroy / process shutdown). We launch
T8b949e// on a fresh detached scope — not the orchestrator's per-start scope — so the subsequent
T8b949e// scope.cancel() below doesn't interrupt the short drain delay inside disconnect(). The
T8b949e// coroutine is fire-and-forget; typical runtime is ~100-150ms which comfortably fits
T8b949e// inside Android's onDestroy() grace window.
Te6edf3CoroutineScopeTb4b4b4(Te6edf3SupervisorJobTb4b4b4(Tb4b4b4) Tff7b72+ Te6edf3dispatchersTb4b4b4.Te6edf3defaultTb4b4b4)Tb4b4b4.Te6edf3launch Tb4b4b4{
Te6edf3runCatching Tb4b4b4{ Te6edf3radioInterfaceServiceTb4b4b4.Te6edf3disconnectTb4b4b4(Tb4b4b4) Tb4b4b4}
Tb4b4b4}
Te6edf3scopeRefTb4b4b4.Te6edf3getAndSetTb4b4b4(Tff7b72nullTb4b4b4)Tff7b72?.Te6edf3cancelTb4b4b4(Tb4b4b4)
Tb4b4b4}
Tb4b4b4}

Served by rngit 1.5.2 - Generated in 0.12s